package at.evonfc.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import at.evonfc.bl.MenuUtils;
import at.evonfc.data.Cart;
import at.evonfc.data.Device;
import at.evonfc.data.Equipment;
import at.evonfc.listofdeviceswithnfc.R;

public class EquipmentActivity extends Activity {
	
	private Map<CheckBox, Equipment> checkboxes = new HashMap<CheckBox, Equipment>();
	public static final String EXTRA_EQUIPMENTS = "EQUIPMENTS";
	private ArrayList<Equipment> selectedEquipment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_equipment);
		
		Intent intent = getIntent();
		selectedEquipment = (ArrayList<Equipment>) intent.getSerializableExtra(EXTRA_EQUIPMENTS);
		//no selected equipment passed
		if(selectedEquipment == null) {
			selectedEquipment = new ArrayList<Equipment>();
		}
		//get the TextView and change the name of the device		
		TextView view = (TextView)findViewById(R.id.tvBorrowedDeviceName);

		//display the name of the current device
		view.setText(getString(R.string.tv_borrowDeviceName).replace("#device#", Cart.getActiveCartDevice().getDevice().getName()));
		
		/* show the available equipment for this device */
		showEquipment();
	}
	
	private void showEquipment() {
		/* get the current device */
		Device device = MainActivity.currDevice;
		/* get the available equipment for this device */
		List<Equipment> equipment = device.getAvailableEquipment();
		/* get the scrollview to add the equipment */
		ScrollView scrollView = (ScrollView) findViewById(R.id.svEquipmentView);
		scrollView.removeAllViews();
		/* the layout which contains the checkboxes */
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		/* show every equipment and a checkbox for this equipment */
		for(final Equipment eq: equipment) {
			CheckBox cbEquipment = new CheckBox(this);
			cbEquipment.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					if(isChecked) {
						selectedEquipment.add(eq);
					} else {
						selectedEquipment.remove(eq);
					}
				}
			});
			cbEquipment.setButtonDrawable(this.getResources().getDrawable(R.drawable.selector));
			cbEquipment.setHeight(80);
			cbEquipment.setText("\t"+eq.getDescription());
			cbEquipment.setTextSize(20);
			cbEquipment.setContentDescription("cb_" + eq.getDescription());
			cbEquipment.setTextColor(Color.WHITE);
			if(selectedEquipment != null) {
				for(Equipment eq1: selectedEquipment) {
					if(eq.getEquipmentId() == eq1.getEquipmentId()) {
						cbEquipment.setChecked(true);
						break;
					}
				}
			}
			checkboxes.put(cbEquipment, eq);
			layout.addView(cbEquipment);
		}
		scrollView.addView(layout);
	}

	@Override
	protected void onResume() {
		super.onResume();
		MainActivity.active_activity = BorrowActivity.ACTIVITY_NAME;
	}
	
	public void onConfirmEquipment(View view) {
		List<Equipment> eq = new ArrayList<Equipment>();
		for(Entry<CheckBox, Equipment> entry : checkboxes.entrySet()) {
			if(entry.getKey().isChecked()) {
				eq.add(entry.getValue());
			}
		}
		Cart.setDeviceEquipment(eq);
		Intent intent = new Intent(this, BorrowDateActivity.class);
		startActivity(intent);
	}
	
	public void onLogout(MenuItem item) {
		MenuUtils.getInstance().onLogout(this);
	}
	
	public void onCartClick(MenuItem item) {
		MenuUtils.getInstance().onCartClick(this);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    if(Cart.hasElements()) {
	    	inflater.inflate(R.menu.cart_new, menu);
	    } else {
	    	inflater.inflate(R.menu.cart_logout, menu);
	    }
	    return true;
	}
}
